/* 
c 实现数据结构: 栈 stack
风格: c++ 类, 成员函数调用
 */

#include "stack.h"
#include <stdio.h>
#include <string.h>

bool empty(struct stack *s)
{
    if (s->top == -1)
        return true;
    return false;
}

bool full(struct stack *s)
{
    if (s->top == s->size)
        return true;
    return false;
}

bool push(struct stack *s, int value)
{
    if (full(s))
        return false;
    s->data[++s->top] = value;
    return true;
}

bool pop(struct stack *s)
{
    if (empty(s))
        return false;
    s->top--;
    return true;
}

int peek(struct stack *s)
{
    if (empty(s))
        return -1;

    return s->data[s->top];
}

void show(struct stack *s)
{
    for (int i = 0; i <= s->top; i++)
    {
        printf("%d ", s->data[i]);
    }
    printf("\n");
}

bool stack_init(struct stack *s)
{
    memset(s, 0, sizeof(struct stack));
    s->top = -1;
    s->size = STACK_SIZE;
    s->full = full;
    s->empty = empty;
    s->push = push;
    s->pop = pop;
    s->peek = peek;
    s->show = show;

    return true;
}